package com.lostandfound.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.lostandfound.entity.User;
import com.lostandfound.service.ItemService;
import com.lostandfound.service.PlateService;
import com.lostandfound.service.ThanksService;
import com.lostandfound.service.TopicService;
import com.lostandfound.service.UserService;
import com.lostandfound.util.ReturnCode;
import com.opensymphony.xwork2.ActionContext;

/**  
* 
*  
* @author xuao  
* @date 2018年3月9日
*/
@ParentPackage("basePackage")
//@Action(value="login")//使用@action注解可以将一个普通类标注为可以处理用户请求的action
@Namespace("/")
@Results({
	@Result(name="nomalLoginSuccess",location="/jsp/nomal/index.jsp"),
	@Result(name="managerLoginSuccess",location="/jsp/manager/index.jsp"),
	@Result(name="loginFail",location="/login.jsp")})
public class LoginAction {
	
	@Autowired
	private UserService userServiceImpl;
	
	
	private User user;
	private String action;
	private HashMap<String, String> map;
	
	public HashMap<String, String> getMap() {
		return map;
	}
	public void setMap(HashMap<String, String> map) {
		this.map = map;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}

	public String login(){
		System.out.println("--------");
		System.out.println(map.get("name"));
		if(this.user != null){
			User user1 = userServiceImpl.selectByPrimary(new Long(1));
			System.out.println("---"+user1.getUserCode());
			System.out.println("****"+user.getUserCode());
			if(user1.getUserCode().equals(this.user.getUserCode()) &&
					user1.getPassword().equals(this.user.getPassword())){
				System.out.println("登录成功！");
				Map<String, Object> session = ActionContext.getContext().getSession();
				session.put("user", user);
				if(this.action == "nomal")
					return "nomalLoginSuccess";
				return "managerLoginSuccess";
			}
		}
		System.out.println("登录失败！");
		return "loginFail";
	}

	
}
